import { ActivityFormBO, ActivityInfoVO, ActivityUpdateDTO, saveActivity } from '@/services/ActivityService'
import { UploadFile } from 'antd'
import { makeAutoObservable } from 'mobx'
import { Area } from 'react-easy-crop'

class ActivityModel {
  activity?: ActivityFormBO
  updateModalState = false
  cropModalState = false
  imgUrl?: string
  cropZoom = 1
  crop = { x: 0, y: 0 }
  cropArea?: Area
  cropStr: string = ''
  posterFileList: UploadFile[] = []

  setPosterFileList(uploadFileList: UploadFile[]) {
    console.log(uploadFileList)
    this.posterFileList = uploadFileList
  }

  constructor() {
    makeAutoObservable(this, {}, { autoBind: true })
  }

  setImgUrl(url: string) {
    this.imgUrl = url
  }

  setUpdateModalState(state: boolean) {
    this.updateModalState = state
  }

  closeCropModal() {
    this.cropModalState = false
  }

  openCropModal() {
    this.cropModalState = true
  }

  openUpdateModal(state: boolean, record: ActivityFormBO) {
    this.activity = {
      ...record,
    }
    this.updateModalState = state
  }

  setZoom(zoom: number) {
    this.cropZoom = zoom
  }

  setCrop(crop: { x: number; y: number }) {
    this.crop = crop
  }

  setCropArea(area: Area) {
    this.cropArea = area
  }

  async saveActivity(bo: ActivityFormBO) {
    let dto: ActivityUpdateDTO = {
      activityName: bo.activityName || '',
      activityTitle: bo.activityTitle || '',
      bindWaId: bo.bindWaId || '',
      bindWpId: bo.bindWpId || '',
      countValueType: JSON.stringify({
        firstSubscribe: bo.firstSubscribe,
        firstEnter: bo.firstEnter,
        firstShare: bo.firstShare,
      }),
      endTime: bo.endTime || '',
      id: bo.id,
      qrPos: this.cropStr,
      shareCover: bo.shareCover || '',
      sharePoster: this.imgUrl || '',
      shareTitle: bo.shareTitle || '',
      startTime: bo.startTime || '',
      posterList: this.posterFileList.map((value) => {
        if (value.url !== undefined) return { picUrl: value.url, fileName: value.name }
        return {
          picUrl: value.response.data[0].url,
          fileName: value.response.data[0].name,
        }
      }),
    }
    console.log(JSON.stringify(dto))
    await saveActivity(dto).then()
    return true
  }

  cropModalOk() {
    this.cropStr = JSON.stringify(this.cropArea)
    this.cropModalState = false
  }

  setActivityBO(vo: ActivityInfoVO) {
    console.log(vo)
    this.imgUrl = vo.sharePoster
    this.cropStr = vo.qrPos
    this.crop = JSON.parse(vo.qrPos)
    this.posterFileList = vo.posters.map(
      (value) =>
        ({
          uid: value.id,
          url: value.picUrl,
          name: value.fileName,
        } as UploadFile),
    )
    let rules = JSON.parse(vo.countValueType)
    this.activity = {
      activityName: vo.activityName,
      activityTitle: vo.activityTitle,
      bindWaId: vo.bindWaId,
      bindWpId: vo.bindWpId,
      countValueType: vo.countValueType,
      endTime: vo.endTime.toString(),
      firstEnter: rules.firstEnter,
      firstShare: rules.firstShare,
      firstSubscribe: rules.firstSubscribe,
      id: vo.id,
      posterList: vo.posters,
      qrPos: vo.qrPos,
      shareCover: vo.shareCover,
      sharePoster: vo.sharePoster,
      shareTitle: vo.shareTitle,
      startTime: vo.startTime.toString(),
    }
  }

  initActivityBO() {
    this.activity = {
      activityName: '',
      activityTitle: '',
      bindWaId: '',
      bindWpId: '',
      countValueType: '',
      endTime: '',
      firstEnter: 0,
      firstShare: 0,
      firstSubscribe: 0,
      id: '',
      posterList: [],
      qrPos: '',
      shareCover: '',
      sharePoster: '',
      shareTitle: '',
      startTime: '',
    }
  }
}

export default ActivityModel
export const activityModel = new ActivityModel()
